package com.yy.jhl.ui.manager.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.yy.common.base.BaseActivity;
import com.yy.common.commonutils.SPUtils;
import com.yy.common.commonutils.StringUtil;
import com.yy.common.commonwidget.OnNoDoubleClickListener;
import com.yy.jhl.R;
import com.yy.jhl.app.AppConstant;
import com.yy.jhl.bean.LogisticsInfo;
import com.yy.jhl.bean.MessageListInfo;
import com.yy.jhl.interfaces.LogisticsCallBack;
import com.yy.jhl.ui.manager.adapter.MessageAdapter;
import com.yy.jhl.ui.manager.contract.MessageListContract;
import com.yy.jhl.ui.manager.presenter.MessageListPresenter;
import com.yy.jhl.util.DataRequestUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by libingn on 2017/12/6.
 */

public class MessageListActivity extends BaseActivity<MessageListPresenter> implements MessageListContract.View, AdapterView.OnItemClickListener, PullToRefreshBase.OnRefreshListener2 {
    @BindView(R.id.message_pull_list_view)
    PullToRefreshListView messagePullListView;
    ListView messageListView;
    MessageAdapter messageAdapter;
    private static int LIMIT = AppConstant.PAGE_SIZE;
    @BindView(R.id.no_data_parent)
    LinearLayout noDataParent;
    private int start = 0;
    //0:未读的 1：已读的
    private int is_history = 0;
    //1 :供应商 2：采购商
    private int currentLoginType = 1;
    private boolean isFirst = true;
    @Override
    public int getLayoutId() {
        return R.layout.act_message_list;
    }

    @Override
    public void initView() {
        currentLoginType = SPUtils.getSharedIntData(mContext, AppConstant.SP_KEY_LOGIN_TYPE);
        showTitle("消息列表", new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });
        messagePullListView.setMode(PullToRefreshBase.Mode.BOTH);
        messageListView = messagePullListView.getRefreshableView();
        messageAdapter = new MessageAdapter(mContext);
        messageListView.setAdapter(messageAdapter);
        messagePullListView.setOnRefreshListener(this);
        messagePullListView.setOnItemClickListener(this);
        mPresenter.getMessageList(start, LIMIT, is_history, true);
    }

    @Override
    public void getMessageListSuccess(MessageListInfo messageListInfo) {
        mPresenter.setMessageReaded();
        messagePullListView.onRefreshComplete();
        if (start == 0) {
            messageAdapter.clearData();
            if (messageListInfo.getData() == null ||messageListInfo.getData().size()== 0){
                noDataParent.setVisibility(View.VISIBLE);
                messagePullListView.setVisibility(View.VISIBLE);
                if (isFirst){
                    isFirst = false;
                    is_history = 1;
                    mPresenter.getMessageList(start, LIMIT, is_history, false);
                }
            }else {
                noDataParent.setVisibility(View.GONE);
                messagePullListView.setVisibility(View.VISIBLE);
            }
        }
        messageAdapter.addData(messageListInfo.getData());
    }

    @Override
    public void getMessageListFail() {
        messagePullListView.onRefreshComplete();
        noDataParent.setVisibility(View.VISIBLE);
        messagePullListView.setVisibility(View.VISIBLE);
    }

    @Override
    public void getMessageResult(boolean isSuccess) {

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        MessageListInfo.DataBean item = (MessageListInfo.DataBean) messageAdapter.getItem(i - 1);
        final String type = item.getType();
//        String objc = item.getObject_type();
        final String id = item.getObject_id();
        if (StringUtil.isEmpty(type))return;
        if ("APPA".equals(type)){//跳转到供应商“任务订单”中该订单的任务单生成页面
                JGOrderDetailActivity.startAction(mContext,id,false);
        }else if ("APPN".equals(type)){//跳转到物流环节子流程的国内/外货代的提交生成页面
            LogisticsManagerActivity.startAction(mContext,id);
        }else if ("APPO".equals(type)){
            OrderFlowActivity.startAction(mContext,id);
        }else if ("APPK".equals(type)|| "APPL".equals(type)|| "APPM".equals(type)){
        }else {
            DataRequestUtil.getInstance(mContext).getLogisticsInfo(id, new LogisticsCallBack() {
                @Override
                public void getCallBack(LogisticsInfo logisticsInfo) {
                    String status = logisticsInfo.getStatus();
                    switch (type){
                        case "APPC"://跳转到物流环节中供应商提交的出库准备页面
                            OutboundCheckActivity.startAction(mContext, logisticsInfo);
//                            OutboundViewActivity.startAction(mContext, logisticsInfo, status);
                            break;
                        case "APPD"://跳转到物流环节中供应商出库准备生成页面
                            OutboundViewActivity.startAction(mContext, logisticsInfo, status);
                            break;
                        case "APPE"://跳转到物流环节中供应商出库准备生成页面
                            OutboundViewActivity.startAction(mContext, logisticsInfo, status);
                            break;
                        case "APPF"://跳转到物流环节中供应商出库准备生成页面
                            OutboundViewActivity.startAction(mContext, logisticsInfo, status);
                            break;
                        case "APPG"://跳转到物流环节中供应商提交的出库页面
                            OutboundAfterCheckActivity.startAction(mContext, logisticsInfo, status);
                            break;
                        case "APPH"://跳转到物流环节中供应商出库生成页面
                            OutboundAfterCheckActivity.startAction(mContext, logisticsInfo, status);
                            break;
                        case "APPI"://跳转到物流环节中供应商出库生成页面
                            OutboundAfterCheckActivity.startAction(mContext, logisticsInfo, status);
                            break;
                        case "APPJ"://跳转到物流环节中供应商出库生成页面
                            OutboundAfterCheckActivity.startAction(mContext, logisticsInfo, status);
                            break;
                    }
                }
            });

        }
//        if (StringUtil.isEmpty(objc))return;
//        switch (objc){
//            case "1":
//                //任务监管单
//                if (currentLoginType == 1){
//                    JGOrderDetailActivity.startAction(mContext,id,true);
//                }else {
//                    JGOrderDetailActivity.startAction(mContext,id,false);
//                }
//                break;
//            case "2":
//                //批次订单
//                LogisticsManagerActivity.startAction(mContext,id);
//                break;
//        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        //refresh
        messageAdapter.clearData();
        start = 0;
//        is_history = 0;
        mPresenter.getMessageList(start, LIMIT, is_history, false);
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        //refresh
        start += LIMIT;
        mPresenter.getMessageList(start, LIMIT, is_history, false);
    }
}
